package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.TokenInfo;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.service.ShoppingCartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shopping")
public class ShoppingCartsController {

    @Autowired
    private ShoppingCartsService cartsService;

    @PutMapping("carts")
    public Object updateCarts(@RequestBody CartRequest cart) {
        cartsService.updateCarts(cart);
        return new ResponseUtil<>().setData(null);
    }

    @DeleteMapping("carts/{userId}/{ProductId}")
    public Object deleteCarts(@PathVariable Long userId, @PathVariable Long ProductId) {
        cartsService.deleteCarts(userId, ProductId);
        return new ResponseUtil<>().setData(null);
    }

    @DeleteMapping("items/{userId}")
    public Object deleteCartsChecked(@PathVariable Long userId) {
        cartsService.deleteCartsChecked(userId);
        return new ResponseUtil<>().setData(null);
    }

    @PutMapping("items")
    public Object selectAllCarts(@RequestBody CartRequest cart) {
        cartsService.selectAllCarts(cart);
        return new ResponseUtil<>().setData(null);
    }

    // @PostMapping("carts")
    public Object addCarts(@RequestBody CartRequest cart) {
        cartsService.addCarts(cart);
        return new ResponseUtil<>().setData(null);
    }

    // @GetMapping("carts")
    public Object queryCarts(@RequestHeader String user_info) {
        TokenInfo tokenInfo = JSON.parseObject(user_info, TokenInfo.class);
        Long uid = tokenInfo.getUid();
        List<CartProductDto> productDtoList = cartsService.queryCarts(uid);
        return new ResponseUtil<>().setData(productDtoList);
    }
}
